<template>
  <section>
    <!-- 面包屑 -->
 <breadcrumbs></breadcrumbs>
    <div class="top-box">
      <div class="top-item"><img src="../../assets/images/zongdingdanshu.png"
             alt=""
             class="top-icon">
        <div class="right-item-box">
          <p><span class="fontTop">{{topData.totalCount}}</span><br>
            <span class="fontText">总订单数</span>
          </p>
        </div>
      </div>
      <div class="top-item"><img src="../../assets/images/daishenhedingdanshu.png"
             alt=""
             class="top-icon">
        <div class="right-item-box">
          <p><span class="fontTop">{{topData.checkIngCount}}</span><br>
            <span class="fontText">待审核订单数</span>
          </p>
        </div>
      </div>
      <div class="top-item"><img src="../../assets/images/shenhechenggongdingdanshu.png"
             alt=""
             class="top-icon">
        <div class="right-item-box">
          <p><span class="fontTop">{{topData.checkEndCount}}</span><br>
            <span class="fontText">审核成功订单数</span>
          </p>
        </div>
      </div>
      <div class="top-item"><img src="../../assets/images/shenheshibaidingdanshu.png"
             alt=""
             class="top-icon">
        <div class="right-item-box">
          <p><span class="fontTop">{{topData.checkBadCount}}</span><br>
            <span class="fontText">审核失败订单数</span>
          </p>
        </div>
      </div>
    </div>
    <!-- 查询条件 -->
    <el-form ref="searchForm"
             :model="searchForm"
             label-width="100px"
             inline
             class="demo-form-inline"
             size='small'
             style="border-bottom: 1px solid #f6f6f6;">
      <el-form-item label="回收站城市">
        <el-select v-model="searchForm.cid"
                   placeholder="请选择回收城市">
          <el-option v-for="(item,index) in cityData"
                     :key="index"
                     :value="item.cityid"
                     :label="item.cityname"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="回收站名称">
        <el-input v-model="searchForm.name"
                  placeholder="请输入回收站名称"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="searchForm.sid"
                   placeholder="请选择状态">
          <el-option label="待审核"
                     :value="0"></el-option>
          <el-option label="审核成功"
                     :value="1"></el-option>
          <el-option label="审核失败"
                     :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="电话号码"
                    prop="">
        <el-input v-model="searchForm.phone"
                  placeholder="请输入电话号码"></el-input>
      </el-form-item>
      <el-form-item label="订单号"
                    prop="">
        <el-input v-model="searchForm.orderNum"
                  placeholder="请输入订单号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
                   @click="search">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>
    <div>
      <p>总订单数&nbsp;&nbsp;共计<span>{{searchForm.totalCount}}条</span></p>
    </div>
    <div class="data-container scroll-table">
      <!-- 按钮列表 -->
      <el-table :data="tableData"
                border
                style="width: 100%"
                @selection-change="tableSelectionChange">
        <!-- <el-table-column type="selection"
                         width="40">
        </el-table-column> -->
        <el-table-column prop="backOrderNo"
                         label="订单号码">
        </el-table-column>
        <el-table-column prop="phoneNum"
                         label="手机号码">
        </el-table-column>
        <el-table-column prop="brandName"
                         label="品牌">
        </el-table-column>
        <!-- <el-table-column prop="backSkuId"
                         label="规格">
        </el-table-column> -->
        <el-table-column prop=""
                         label="数量">
               <template slot-scope="scope">
                <div v-if="scope.row.state==0">{{scope.row.backCount}}</div>
                <div v-if="scope.row.state==1">{{scope.row.realBackCount}}</div>
                <div v-if="scope.row.state==2">{{scope.row.backCount}}</div>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="price"
                         label="单价">
        </el-table-column> -->
        <el-table-column prop=""
                         label="总计金额(元)">
               <template slot-scope="scope">
                <div v-if="scope.row.state==0">{{scope.row.backMoney}}</div>
                <div v-if="scope.row.state==1">{{scope.row.realBackMoney}}</div>
                <div v-if="scope.row.state==2">{{scope.row.backMoney}}</div>
          </template>
        </el-table-column>
        <el-table-column prop="addtime"
                         label="提交时间">
        </el-table-column>
        <el-table-column prop="checkTime"
                         label="审核时间">
        </el-table-column>
        <el-table-column prop="stopName"
                         label="审核回收站点">
        </el-table-column>
        <el-table-column prop="checkname"
                         label="审核人">
        </el-table-column>
        <el-table-column prop="state"
                         label="状态">
          <template slot-scope="scope">
            <div v-if="scope.row.state==0">审核中</div>
            <div v-if="scope.row.state==1">审核通过</div>
            <div v-if="scope.row.state==2">审核失败</div>
          </template>
        </el-table-column>
        <el-table-column prop="state"
                         label="操作">
          <template slot-scope="scope">
            <el-button @click="showDetails(scope.row)">查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination class="mgt-10 pd-0"
                     @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="searchForm.current"
                     :page-sizes="[10, 20, 30, 50]"
                     :page-size="searchForm.size"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="searchForm.totalCount">
      </el-pagination>
    </div>
    <!--新增修改界面-->

  </section>
</template>

<script>
import breadcrumbs from "@/components/breadcrumbs.vue";
import btns from '@/components/btns.vue'
import { format } from "../../utils/tool"

export default {
  components: {
    btns,
    breadcrumbs,
  },
  data () {
    return {
      addForm: {},
      yearData: "",
      topData: {},
      btnForm: {},
      searchForm: {
        current: 1,
        size: 10,
        info: "",
        sid: "",
        name:'',
        phone:'',
        orderNum:''
      },
      cityData: [],
      addORUpdateVisible: false,
      funTit: '',
      cityForm: {
        cityName: "",
        cityId: ''
      },
      rules: {
        "btnIcon": [],
        "btnName": [
          { required: true, message: '请输入城市名称', trigger: 'blur' }
        ],
        "btnCode": [
          { required: true, message: '请选择状态', trigger: 'blur' }
        ],
        "sort": [
          { required: true, message: '排序', trigger: 'blur' }
        ]
      },
      tableData: [],
      tableSelection: []
    }
  },
  created () {
    // this.btnlist();
    this.getYearData();
    this.getCityList();
    this.getTableData(1, 10);
  },
  methods: {
    showDetails (row) {
      this.$router.push({
        path: "/order/orderDetails",
        query: {
          backOrderId: row.backOrderId
        }
      })
    },
    getCityList () {
      this.$api.city.getList({
        current: 1,
        size: 10000
      }).then(d => {
        if (d.code == 200) {
          this.cityData = d.data;
        } else {
          this.$message.error(d.msg);
        }
      }).catch((e) => {
        console.log(e);
      })
    },
    // showDetails (row) {
    //   this.$router.push({
    //     path: "/user/userDetails",
    //     query: {
    //       userId: row.wxUserId
    //     }
    //   })
    // },
    handleSizeChange (v) {
      console.log(v)
      this.searchForm.size = v;
      this.getTableData(this.searchForm.current, v);
    },
    handleCurrentChange (v) {
      this.searchForm.current = v;
      this.getTableData(v, this.searchForm.size);
    },
    getTableData (curr, size, name, sid, cid, phone, orderNum) {
      this.$api.order.getAdminBackOrderList({
        current: curr,
        size: size,
        cityId: cid,
        phone: phone,
        stopName: name,
        state: sid,
        orderNo: orderNum,
      }).then(res => {
        if (res.code == 200) {
          console.log(res.data)
          let result = res.data.page.records;
          let brand = res.data.skulist;
          result.forEach((item) => {
            brand.forEach((op) => {
              console.log(item, op)
              if (item.backOrderId == op.backOrderId) {
                // item.brandName = op.brandName;
                // item.backSkuId = op.backSkuId;
                item.backCount = op.backCount;
                item.price = op.price;
                item.total = parseFloat(op.price) * parseFloat(op.backCount)
              }
            })
          })
          this.tableData = result;
          this.searchForm.totalCount = res.data.page.total;
          this.searchForm.size = res.data.page.size
        }
      })
    },
    chageYearData () {
      this.$api.user.getAdminUserCount({
        year: this.yearData
      }).then(res => {
        if (res.code == 200) {
          this.topData = res.data
        }
      })
    },
    getYearData () {
      this.$api.order.getAdminBackOrderCount().then(res => {
        if (res.code == 200) {
          this.topData = res.data;
        }
      })
    },
    handleReset () {
      this.searchForm={
        
        cid: "",
        name: "",
        sid:'',
        phone:'',
        orderNum:''
      },

      // this.$refs.searchForm.resetFields();
      this.getTableData(this.searchForm.current, this.searchForm.size);
    },
    search () {
      console.log(this.searchForm.current, this.searchForm.size, this.searchForm.info, this.searchForm.sid)
      // this.getTableData (curr, size, name, sid, cid, phone, orderNum)
      // getTableData (curr, size, name, sid, cid, phone, orderNum)
      this.getTableData(this.searchForm.current, this.searchForm.size, this.searchForm.name, this.searchForm.sid, this.searchForm.cid, this.searchForm.phone, this.searchForm.orderNum);
    },
    btnlist () {
      //获取接口
      this.$api.city.getList({}).then(d => {
        if (d.code == 200) {
          this.tableData = d.data;
        } else {
          this.$message.error(d.msg);
        }
      }).catch((e) => {
        console.log(e);
      })
    },
    tableSelectionChange (val) {
      this.tableSelection = val;
    },
    exec (fun) {
      if (this[fun]) {
        this.funTit = fun;
        this[fun]()
      } else {
        this.$message.error('方法未定义');
      }
    },
    add () {

      this.btnForm = {
        "btnIcon": "",
        "btnName": "",
        "btnCode": "",
        "sort": 0
      }
      this.addORUpdateVisible = true;
    },
    update () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }
      this.btnForm = this.tableSelection[0];
      this.addORUpdateVisible = true;
    },
    del () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }

    },
    refresh () {
      this.btnlist();
    },
    enable () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }
      var demo = this.tableSelection[0];
      if (demo.state == 1) {
        this.$message.error('该数据已启用');
        return
      }
      this.$confirm('此操作将启用该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.user.updateStatus({ userId: demo.wxUserId, status: 1 }).then(d => {
          if (d.code == 200) {
            this.getTableData(this.searchForm.current, this.searchForm.size, this.searchForm.info, this.searchForm.sid);
            this.$message.success('启用成功');
          } else {
            this.$message.error(d.msg);
          }
        }).catch((e) => {
          console.log(e);
        })
      });
    },
    disable () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }
      var demo = this.tableSelection[0];
      console.log(demo)
      if (demo.state == 2) {
        this.$message.error('该数据已禁用');
        return
      }
      this.$confirm('此操作将禁用该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.user.updateStatus({ userId: demo.wxUserId, status: 2 }).then(d => {
          if (d.code == 200) {
            this.getTableData(this.searchForm.current, this.searchForm.size, this.searchForm.info, this.searchForm.sid);
            this.$message.success('禁用成功');
          } else {
            this.$message.error(d.msg);
          }
        }).catch((e) => {
          console.log(e);
        })
      });
    },
    submit (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.tableData.findIndex(c => c.btnCode == this.btnForm.btnCode) >= 0) {
            this.$message.error('code不能重复');
            return
          }
          if (this.funTit == 'add') {
            this.$api.system.btnadd(this.btnForm).then(d => {
              if (d.code == 200) {
                this.addORUpdateVisible = false;
                this.$refs.btnForm.resetFields();
                this.btnlist();
              } else {
                this.$message.error(d.msg);
              }
            }).catch((e) => {
              console.log(e);
            })
          } else {
            this.$api.system.btnupdate(this.btnForm).then(d => {
              if (d.code == 200) {
                this.addORUpdateVisible = false;
                this.$refs.btnForm.resetFields();
                this.btnlist();
              } else {
                this.$message.error(d.msg);
              }
            }).catch((e) => {
              console.log(e);
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}

</script>
<style lang="less" scoped>
.marbt20px {
  margin-bottom: 20px;
}
.fontText {
  font-size: 20px;
}
.fontTop {
  font-size: 40px;
}
.top-box {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 40px;
}
.top-item {
  border: 1px gray solid;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 20%;
  padding: 20px;
  .top-icon {
    width: 35%;
    height: 35%;
  }
  .right-item-box {
    text-align: center;
    margin-left: 20px;
  }
}
</style>